﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xfs;

namespace Xfs
{
    [XfsObjectSystem]
    public class XfsUnitComponentAwake1System : XfsAwakeSystem<XfsUnitComponent>
    {
        public override void Awake(XfsUnitComponent self)
        {
            self.UnitSpown(1);
        }
    }

    [XfsObjectSystem]
    public class XfsUnitComponentStartSystem : XfsStartSystem<XfsUnitComponent>
    {
        public override void Start(XfsUnitComponent self)
        {

        }
    }

    [XfsObjectSystem]
    public class XfsUnitComponentUpdateSystem : XfsUpdateSystem<XfsUnitComponent>
    {
        public override void Update(XfsUnitComponent self)
        {
           

        }
    }

    public static class XfsUnitComponentHepler
    {
        public static long GetInstanceId(this XfsUnitComponent self, XfsSession session, long roleid)
        {
            if (self.Idses.TryGetValue(session.RemoteAddress.ToString(), out Dictionary<long, long> inids))
            {
                if (inids.TryGetValue(roleid, out long inid))
                {
                    return inid;
                }
            }
            return 0;
        }

        public static async void SetMonsters(this XfsUnitComponent self, XfsSession session)
        {
            if (self.Units.Count > 0)
            {
                foreach (var tem in self.Units.Values)
                {
                    C4S_Unit request = new C4S_Unit() { Roleid = tem.Id, Roleinstanceid = tem.InstanceId };

                    S4C_Unit resposne = (S4C_Unit)await session.Call(request);

                    if (resposne != null)
                    {
                        self.AddIds(session,resposne);
                    }
                }
            }
        }

        public static void AddIds(this XfsUnitComponent self, XfsSession session, S4C_Unit response)
        {
            if (response.Roleid != 0 && response.Roleinstanceid != 0)
            {
                if (XfsGame.XfsSence.GetComponent<XfsUnitComponent>().Idses.TryGetValue(session.RemoteAddress.ToString(), out Dictionary<long,long> inIds))
                {
                    inIds.Add(response.Roleid, response.Roleinstanceid);

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 79. message.Roleid/Ids/Idses " + response.Roleid + "/" + inIds.Count + "/" + XfsGame.XfsSence.GetComponent<XfsUnitComponent>().Idses.Count);
                }
                else
                {
                    Dictionary<long, long> inIds2 = new Dictionary<long, long>();

                    inIds2.Add(response.Roleid, response.Roleinstanceid);

                    XfsGame.XfsSence.GetComponent<XfsUnitComponent>().Idses.Add(session.RemoteAddress.ToString(), inIds2);

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 89. message.Roleid/Ids/Idses " + response.Roleid + "/" + inIds2.Count + "/" + XfsGame.XfsSence.GetComponent<XfsUnitComponent>().Idses.Count);
                }
            }
        }

        public static void UnitSpown(this XfsUnitComponent self , int unitMaxCount = 4)
        {
            if (XfsGame.XfsSence.Type == XfsSenceType.XfsServer)
            {
                self.unitCount = unitMaxCount;

                for (int i = 0; i < self.unitCount; i++)
                {
                    XfsUnit? unit = XfsComponentFactory.CreateWithId<XfsUnit>(self.unitId + i);

                    if (unit == null) return;

                    unit.AddComponent<XfsMoveComponent>();
                    unit.AddComponent<XfsMailboxComponent>();
                    unit.AddComponent<XfsAstarComponent>();
                    unit.AddComponent<XfsPatrolComponent>();

                    if (i <= 5)
                    {
                        float px1 = (i + 1f) * 6f;
                        if (px1 <= 0)
                        {
                            px1 = 1f;
                        }
                        if (px1 >= 39f)
                        {
                            px1 = 38f;
                        }
                        unit.Position = new Vector3(px1 * 10, 0f, 160f);
                        unit.StartPosition = new Vector3(px1 * 10, 0f, 160f);
                    }
                    else
                    {
                        float px2 = (i - 5f) * 8f;
                        if (px2 <= 0)
                        {
                            px2 = 1f;
                        }
                        if (px2 >= 39f)
                        {
                            px2 = 38f;
                        }
                        unit.Position = new Vector3(px2 * 10, 0f, 260f);
                        unit.StartPosition = new Vector3(px2 * 10, 0f, 260f);
                    }

                    self.Units.Add(unit.Id, unit);

                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 145. Id: " + unit.Id + " . px/py: " + unit.Position.ToString() + "/" + unit.StartPosition.ToString());
                }
                Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType().Name + " 146. Units: " + self.Units.Count);
            }
        }








    }

}